/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.test.app.dao;

import com.test.app.form.Contact;
import java.util.List;
//import org.hibernate.SessionFactory;
import javax.persistence.*;

/**
 *
 * @author darshana
 */
public class ContactDaoImpl implements ContactDao{

    @PersistenceContext
    //private SessionFactory sessionFactory;
    private EntityManager entityManager;


//    @PersistenceContext
//    public void setEntityManager(EntityManager entityManager) {
//        this.entityManager = entityManager;
//    }


    @Override
    public void addContact(Contact contact) {
        entityManager.persist(contact);
        //sessionFactory.getCurrentSession().save(contact);
    }

   
    @Override
    public List<Contact> list() {
        Query query=entityManager.createQuery("FROM Contact");
        return query.getResultList();
        //return sessionFactory.getCurrentSession().createQuery("from Contact").list();
    }

   
    @Override
    public void remove(Integer id) {
        Contact a= entityManager.find(Contact.class, id);
        if(null != a){
            entityManager.remove(a);
        }
        




//        //Contact c=(Contact) sessionFactory.getCurrentSession().load(Contact.class, id);
//        if(null!=c){
//            sessionFactory.getCurrentSession().delete(c);
//        }
    }

}
